﻿using System;
using System.Text;
using System.Collections;
using System.Reflection;
using System.Collections.Generic;


namespace Job.Core.Data
{

    /// <summary>
    /// 
    /// </summary>
    public class QuerySingle
    {
        private string _fileds = string.Empty;
        private SqlAction _sqlaction = SqlAction.Equal;
        private SqlLogic _sqllogic = SqlLogic.And;
        private List<String> _stringlist = new List<String>();
        private List<Boolean> _boollist = new List<Boolean>();
        private List<Int32> _intlist = new List<Int32>();
        private List<DateTime> _datetimelist = new List<DateTime>();

        /// <summary>
        /// 
        /// </summary>
        public List<string> SetstrList
        {
            get { return _stringlist; }
            set { _stringlist = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public List<bool> SetboolList
        {
            get { return _boollist; }
            set { _boollist = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public List<int> SetintList
        {
            get { return _intlist; }
            set { _intlist = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public List<DateTime> SetdatetimeList
        {
            get { return _datetimelist; }
            set { _datetimelist = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public QuerySingle()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        public QuerySingle(string fileds, SqlAction sqla, SqlLogic sqll, List<string> list)
        {
            _fileds = fileds;
            _sqlaction = sqla;
            _sqllogic = sqll;
            _stringlist = list;
        }

        /// <summary>
        /// 
        /// </summary>
        public QuerySingle(string fileds, SqlAction sqla, SqlLogic sqll, List<bool> list)
        {
            _fileds = fileds;
            _sqlaction = sqla;
            _sqllogic = sqll;
            _boollist = list;
        }

        /// <summary>
        /// 
        /// </summary>
        public QuerySingle(string fileds, SqlAction sqla, SqlLogic sqll, List<int> list)
        {
            _fileds = fileds;
            _sqlaction = sqla;
            _sqllogic = sqll;
            _intlist = list;
        }

        /// <summary>
        /// 
        /// </summary>
        public QuerySingle(string fileds, SqlAction sqla, SqlLogic sqll, List<DateTime> list)
        {
            _fileds = fileds;
            _sqlaction = sqla;
            _sqllogic = sqll;
            _datetimelist = list;
        }

        /// <summary>
        /// 
        /// </summary>
        public SqlAction GetSqlAction
        {
            get { return _sqlaction; }
            set { _sqlaction = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string GetSqlActionStr
        {
            get
            {
                string str = _sqlaction.ToString();
                switch (_sqlaction)
                {
                    case SqlAction.NotEqual:
                        str = "!=";
                        break;
                    case SqlAction.Equal:
                        str = "=";
                        break;
                    case SqlAction.GreaterOrEqual:
                        str = ">=";
                        break;
                    case SqlAction.LessOrEqual:
                        str = "<=";
                        break;
                    case SqlAction.Greater:
                        str = ">";
                        break;
                    case SqlAction.Less:
                        str = "<";
                        break;
                }
                return str;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string GetActionValueStr(Int32 obj)
        {
            return obj.ToString(); ;
        }

        /// <summary>
        /// 
        /// </summary>
        public string GetActionValueStr(Boolean obj)
        {
            string str;
            if (obj)
                str = "1";
            else
                str = "0";
            return str;
        }

        /// <summary>
        /// 
        /// </summary>
        public string GetActionValueStr(String obj)
        {
            obj = string.Format("'{0}'", obj.Replace("'", "''"));
            return obj;
        }

        /// <summary>
        /// 
        /// </summary>
        public string GetActionValueStr(DateTime obj)
        {
            string str = string.Format("'{0}'", obj.ToString("yyyy-MM-dd HH:mm:ss"));
            return str;
        }

        /// <summary>
        /// 
        /// </summary>
        public string GetThisQuery
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                bool isFrist = true;
                foreach (String obj in _stringlist)
                {
                    if (isFrist)
                    {
                        isFrist = false;
                        sb.AppendFormat("{0} {1} {2} ", _fileds, GetSqlActionStr, GetActionValueStr(obj));
                    }
                    else
                    {
                        sb.AppendFormat("{0} {1} {2} {3} ", _sqllogic.ToString(), _fileds, GetSqlActionStr, GetActionValueStr(obj));
                    }
                }
                isFrist = true;
                foreach (Int32 obj in _intlist)
                {
                    if (isFrist)
                    {
                        isFrist = false;
                        sb.AppendFormat("{0} {1} {2} ", _fileds, GetSqlActionStr, GetActionValueStr(obj));
                    }
                    else
                    {
                        sb.AppendFormat("{0} {1} {2} {3} ", _sqllogic.ToString(), _fileds, GetSqlActionStr, GetActionValueStr(obj));
                    }
                }
                isFrist = true;
                foreach (Boolean obj in _boollist)
                {
                    if (isFrist)
                    {
                        isFrist = false;
                        sb.AppendFormat("{0} {1} {2} ", _fileds, GetSqlActionStr, GetActionValueStr(obj));
                    }
                    else
                    {
                        sb.AppendFormat("{0} {1} {2} {3} ", _sqllogic.ToString(), _fileds, GetSqlActionStr, GetActionValueStr(obj));
                    }
                }
                isFrist = true;
                foreach (DateTime obj in _datetimelist)
                {
                    if (isFrist)
                    {
                        isFrist = false;
                        sb.AppendFormat("{0} {1} {2} ", _fileds, GetSqlActionStr, GetActionValueStr(obj));
                    }
                    else
                    {
                        sb.AppendFormat("{0} {1} {2} {3} ", _sqllogic.ToString(), _fileds, GetSqlActionStr, GetActionValueStr(obj));
                    }
                }
                sb.AppendFormat("({0})", sb.ToString());
                return sb.ToString();
            }
        }

    }
    
    /// <summary>
    /// 
    /// </summary>
    public enum SqlAction
    {
        Equal,
        NotEqual,
        GreaterOrEqual,
        LessOrEqual,
        Greater,
        Less,
        Like,
        Contains,
    }

    /// <summary>
    /// 
    /// </summary>
    public enum SqlLogic
    {
        And,
        Or,
    }
}